package net.bwie.jtp.dws.log.job;

import com.alibaba.fastjson.JSON;
import net.bwie.jtp.dws.log.bean.PageViewBean;
import net.bwie.jtp.dws.log.funcion.PageViewBeanMapFunction;
import net.bwie.jtp.dws.log.funcion.PageViewReportReduceFunction;
import net.bwie.jtp.dws.log.funcion.PageViewReportWindowFunction;
import net.bwie.jtp.dws.log.funcion.PageViewWindowFunction;
import net.bwie.realtime.guanjuntao.util.JdbcUtil;
import net.bwie.realtime.guanjuntao.util.KafkaUtil;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.streaming.connectors.kafka.FlinkKafkaProducer;

import java.time.Duration;

/**
 * @author: 🌹帅的一塌糊涂🌹
 * @description:
 * @params: 日志实统计,
 *          粒度:ar地区,ba品牌,ch渠道,is_new新老用户
 *          指标:pv(页面浏览数),uv(独立访客数),sv(会话数),during_time(会话时长)
 * @return:
 * @date: 2025/5/19 15:49
 */
public class JtpTarfficPageViewMinuyeWondowDwsjob {
    public static void main(String[] args) throws Exception {
        // TODO: 2025/5/19 创建flink上下文,执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        // TODO: 2025/5/19 获取数据page数据(kafak)
        DataStream<String> pageStream = KafkaUtil.consumerKafka(env, "dwd-traffic-page-log");
        // TODO: 2025/5/19 数据转换
        DataStream<String> resultStream = handle(pageStream);
        // TODO: 2025/5/19  输出数据sink(clickhouse实时数据库)
//        JdbcUtil.sinkToClickhouseUpsert(
//         resultStream,"insert into jtp_log_report.dws_log_page_view_window_report(\n" +
//        "window_start_time, window_end_time, brand, channel, province,\n" +
//        "is_new, pv_count, pv_during_time, uv_count, session_count,\n" +
//        "ts)\n" +
//        "values (?,?,?,?,?,?,?,?,?,?,?);"
//        );
resultStream.print();

        // TODO: 2025/5/19  触发执行
        env.execute("JtpTarfficPageViewMinuyeWondowDwsjob");

    }

    private static DataStream<String> handle(DataStream<String> pageStream) {
        // TODO: 2025/5/19 根据mid分组,用于计算uv,使用状态编程记录今日是否是第一次访问
        KeyedStream<String, String> midStream = pageStream.keyBy(json -> JSON.parseObject(json)
                .getJSONObject("common").getString("mid"));
        // TODO: 2025/5/19 将流中数据封装到实体类
        DataStream<PageViewBean> beanStream =
                midStream.map(new PageViewBeanMapFunction());
        // TODO: 2025/5/19 设置水位线
        DataStream<PageViewBean> timeStreamm = beanStream.assignTimestampsAndWatermarks(
                // TODO: 2025/5/19 允许乱序最的时间是0秒
                WatermarkStrategy.<PageViewBean>forBoundedOutOfOrderness(Duration.ofSeconds(0)).withTimestampAssigner(
                        new SerializableTimestampAssigner<PageViewBean>() {
                            @Override
                            public long extractTimestamp(PageViewBean element, long recordTimestamp) {
                                // TODO: 2025/5/19 返回数据中的时间戳
                                return element.getTs();
                            }
                        }
                ));
        // TODO: 2025/5/19  对ar(地区) ba(品牌) ch(渠道) is_new(新老访客数)进行分组
        KeyedStream<PageViewBean, String> keybyStream = timeStreamm.keyBy(bean -> bean.getBrand() + "," + bean.getChannel() + "," + bean.getProvince() + "," + bean.getIsNew());
        // TODO: 2025/5/19  开启滚动窗口,时间为1分钟
        WindowedStream<PageViewBean, String, TimeWindow> windowStream = keybyStream.window(TumblingEventTimeWindows.of(Time.minutes(1)));
        // TODO: 2025/5/19 聚合
//        DataStream<String> apply = windowStream.apply(new PageViewWindowFunction());
        // TODO: 2025/5/20 增强 
        SingleOutputStreamOperator<String> reduce = windowStream.reduce(new PageViewReportReduceFunction(), new PageViewReportWindowFunction());
        return reduce;
    }
}








































































